package com.androidserver.server;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.androidserver.util.DatastoreWrapper;
import com.google.appengine.api.datastore.Entity;

@SuppressWarnings("serial")
public class MakeGroup extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		if (!req.getHeader("User-Agent").contains("Android")) {
			resp.getWriter().print("No browser access");
			return;
		}
		DatastoreWrapper dataWrapper = DatastoreWrapper.getInstance();
		String groupName = req.getParameter("groupName");
		String idCode = req.getParameter("idCode");

		Entity member = dataWrapper.getMemberByID(idCode);
		Entity group = dataWrapper.getGroup(groupName);
		
		if (member != null && group == null) {
			group = new Entity("Group", DatastoreWrapper.GROUPS_KEY);
			group.setProperty("groupName", groupName);
			group.setProperty("groupOwnerID", member.getProperty("idCode").toString());
			group.setProperty(idCode, idCode);
			dataWrapper.addGroup(group);
			resp.getWriter().print("OK");
		} else resp.getWriter().print("FAIL");
		
	}
}
